﻿namespace ServerBuildConsole
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using System.Windows.Media;
    using ServerBuildConsole.Views.ChildWindows;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            int size = 16;
            int ColumnCount = 0;
            StackPanel newPanel = new StackPanel { Orientation = Orientation.Horizontal };
            for (int i = 0; i < size; i++)
            {
                
                Border tBorder = new Border() { Width = 200, Height = 120, Margin = new System.Windows.Thickness(10), BorderThickness = new System.Windows.Thickness(1), BorderBrush = new SolidColorBrush(Colors.Gray) };
                StackPanel tPanel = new StackPanel() { Name = i.ToString() };
                tPanel.MouseEnter +=new System.Windows.Input.MouseEventHandler(BuildStackPanel_MouseEnter);
                tPanel.MouseLeave += new System.Windows.Input.MouseEventHandler(BuildStackPanel_MouseLeave);
                tPanel.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(BuildStackPanel_MouseLeftButtonDown);

                TextBlock tNameBlock = new TextBlock() { Height = 23, Text = i.ToString(), VerticalAlignment = System.Windows.VerticalAlignment.Top, TextAlignment = System.Windows.TextAlignment.Center, Margin = new System.Windows.Thickness(10), FontSize = 16, Foreground = new SolidColorBrush(Colors.Purple) };
                TextBlock tDescriptionBlock = new TextBlock() { Height = 23, Text = "This build is designed for the highest performance available.", VerticalAlignment = System.Windows.VerticalAlignment.Top, TextAlignment = System.Windows.TextAlignment.Center, Margin = new System.Windows.Thickness(10), TextWrapping = System.Windows.TextWrapping.Wrap };
                TextBlock tCostBlock = new TextBlock() { Height = 23, Text = "Cost:  $200 per server", VerticalAlignment = System.Windows.VerticalAlignment.Top, TextAlignment = System.Windows.TextAlignment.Center, Margin = new System.Windows.Thickness(10), TextWrapping = System.Windows.TextWrapping.Wrap};

                tPanel.Children.Add(tNameBlock);
                tPanel.Children.Add(tDescriptionBlock);
                tPanel.Children.Add(tCostBlock);

                tBorder.Child = tPanel;

                newPanel.Children.Add(tBorder);
                ColumnCount++;

                if (ColumnCount == 3 || i == size - 1)
                {
                    ColumnCount = 0;
                    this.panelMaster.Children.Add(newPanel);
                    newPanel = new StackPanel { Orientation = Orientation.Horizontal };
                }
            }
        }
        private void BuildStackPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            removeHoverOverBackground((StackPanel)sender);
            BuildDetails details = new BuildDetails();
            details.Show();
        }

        private void GoldBuildStackPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            removeHoverOverBackground((StackPanel)sender);
            BuildDetails details = new BuildDetails();
            details.Show();
        }

        private void BuildStackPanel_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            setHoverOverBackground((StackPanel)sender);
        }

        private void BuildStackPanel_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            removeHoverOverBackground((StackPanel)sender);
        }

        private void setHoverOverBackground(StackPanel sender)
        {
            sender.Background = new SolidColorBrush(Colors.LightGray);
        }

        private void removeHoverOverBackground(StackPanel sender)
        {
            sender.Background = new SolidColorBrush(Colors.White);
        }

        private void CustomBuildStackPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            removeHoverOverBackground((StackPanel)sender);
            BuildDetails details = new BuildDetails();
            details.Show();
        }

    }
}